package xiGua

import (
	log2 "answer5/log"
	"answer5/unti"
	"encoding/json"
	"github.com/golang/protobuf/proto"
	"github.com/gorilla/websocket"
	"log"
	"strconv"
	"strings"
	"sync"
)

var (
	wssUrls []string
	connections []connection
	connectionLen int
	err error
	counter int
	lock         sync.Mutex

	isStart *bool
	curRound int32
	IsTest = false
)

func Start(start *bool){
	go log2.LogPrint("开始西瓜")
	isStart = start

	wssUrls = []string{
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70372040883&iid=97992221409&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=e8540c8ae6d1430e09ef9de880580a34&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?sdk_version=2&platform=0&fpid=90001&version_code=81606&iid=98105904667&device_id=70390672706&aid=32&zpw=0&version=510&access_key=bc4adc4f77514ba118ed728c1e26ddcb&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?sdk_version=2&platform=0&fpid=90001&version_code=81606&iid=98107111475&device_id=70390737655&aid=32&zpw=0&version=510&access_key=f18d3155afe5e4e446901c2d9a7d9584&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?sdk_version=2&platform=0&fpid=90001&version_code=81606&iid=98107604413&device_id=70390755171&aid=32&zpw=0&version=510&access_key=d3feb43a2e559e7aa8c25b95a8550f5e&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?sdk_version=2&platform=0&fpid=90001&version_code=81606&iid=98108034773&device_id=70390770482&aid=32&zpw=0&version=510&access_key=528a8a9634e3c259de6a6757ae26f436&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?sdk_version=2&platform=0&fpid=90001&version_code=81606&iid=98108452837&device_id=70390792133&aid=32&zpw=0&version=510&access_key=576947768656704478bb8631e09f87fa&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?sdk_version=2&platform=0&fpid=90001&version_code=81606&iid=98108620418&device_id=70390797443&aid=32&zpw=0&version=510&access_key=d81e4bc66caa6cf797c8b4f183269151&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?sdk_version=2&platform=0&fpid=90001&version_code=81606&iid=98109038946&device_id=70390821721&aid=32&zpw=0&version=510&access_key=723efb6b30382d8f798836f037442026&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?sdk_version=2&platform=0&fpid=90001&version_code=81606&iid=98109053626&device_id=70390816195&aid=32&zpw=0&version=510&access_key=475a9adcbe9016c8d790a76f5b0925e5&ne=1",

		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492373169&iid=99837558011&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=e7e87d9401b88ec5f93972b084a9a009&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492434987&iid=99838507277&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=ebbc4520bde79ecea99ac264ba2ee1f2&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492472390&iid=99839207759&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=19bf98e8b722339aa9464c110d9add89&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492486050&iid=99839710437&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=9c50413d364508b9f1dbb888bf8d603c&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492502477&iid=99840166563&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=20fbdb70771bab4eb7bb5678a41ab88e&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492524106&iid=99840593048&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=bd6065706a9ef5d274f417ed139c05cd&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492534526&iid=99840951045&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=4c48912520a9eb7909e6387c3afa4609&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492555343&iid=99841416665&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=0697528ebb7396da7ec3ee45a4d74c74&ne=1",
		"wss://spe-f2-frontier-hl.snssdk.com/ws/v2?version_code=81606&device_id=70492577645&iid=99841903501&platform=0&fpid=90001&sdk_version=2&aid=32&zpw=0&version=510&access_key=3c1c9233e3922a1c419f47d86105cb82&ne=1",

	}

	if IsTest{
		wssUrls = []string{
			"ws://127.0.0.1:8808",
			"ws://127.0.0.1:8808",
		}
	}

	connectionLen = len(wssUrls)
	connections = make([]connection,connectionLen)

	for i,url:=range wssUrls{
		connections[i].Question=unti.Question{}
		connections[i].Socket, _, err = websocket.DefaultDialer.Dial(url, nil)
		if err != nil {
			log.Println("socket:",err)
			go log2.ErrorPrint(err.Error())
		}else {
			go log2.LogPrint("启动websocket:"+strconv.Itoa(i))
			go getQuestion(i)
		}
	}
}
func Stop(start *bool){
	isStart = start
	for _, con := range connections {
		if con.Socket != nil {
			con.Socket.Close()
		}
	}
}

func getQuestion(index int){
	var p1 *XiguaQuestionData
	con := &connections[index]

	for{
		if !*isStart{
			break
		}
		_, message, err := con.Socket.ReadMessage()
		if err != nil {
			continue
		}

		p1 = &XiguaQuestionData{}

		if (!IsTest) {
			err = proto.Unmarshal(message, p1)
			if err != nil {
				log.Println(err)
				go log2.ErrorPrint(err.Error())
				continue
			}
		}else{
			err = json.Unmarshal(message,p1)
			if err != nil {
				log.Println(err)
				go log2.ErrorPrint(err.Error())
				continue
			}
		}



		if p1.QuestionMaster.Question != nil{
			//新题目
			if p1.QuestionMaster.Question.Round > curRound{
				lock.Lock()
				curRound = p1.QuestionMaster.Question.Round
				lock.Unlock()
				con.Question.Round = p1.QuestionMaster.Question.Round
				con.Question.Title = p1.QuestionMaster.Question.Title
				con.Question.Options = make([]unti.Option,len(p1.QuestionMaster.Question.Options))
				counter = 1
				for _i, option := range p1.QuestionMaster.Question.Options {
					con.Question.Options[_i] = unti.Option{
						Text:   option.Title,
						PMatch: 0,
						FMatch: 0,
						Words:  nil,
					}
				}
				go unti.NewQuestion(&con.Question)
			}else if p1.QuestionMaster.Question.Round > con.Question.Round {
				con.Question.Round = p1.QuestionMaster.Question.Round
				con.Question.Options = make([]unti.Option,len(p1.QuestionMaster.Question.Options))
				if p1.QuestionMaster.Question.Round == curRound {
					counter++
					for _i, option := range p1.QuestionMaster.Question.Options {
						con.Question.Options[_i] = unti.Option{
							Text:  strings.Trim(option.Title," "),
							PMatch: 0,
							FMatch: 0,
							Words:  nil,
						}
					}

					if counter >= connectionLen {
						optGroup := make([]*[]string,connectionLen)
						for _i,_con:=range connections{
							_o:=make([]string,len(_con.Question.Options))
							for _oi,_op := range _con.Question.Options{
								_o[_oi]=_op.Text
							}
							optGroup[_i]=&_o
						}
						go unti.RandomOptions(&optGroup)
					}

				}
			}
		}

	}
}

type connection struct {
	Question unti.Question
	Socket *websocket.Conn
}